﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Model.Database;
using SearchModel;

namespace Bll
{
    public partial class RoleBll
    {
        /// <summary>
        /// 条件分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="smRole"></param>
        /// <returns></returns>
        public List<Role> GetListBySearchPage(int pageIndex, int pageSize, out int total, SMRole smRole)
        {
            IQueryable<Role> roleIq = CurrentDal.GetIQueryable();

            roleIq = BaseSearch(roleIq, smRole);
            total = roleIq.Count();
            roleIq = roleIq.OrderByDescending(a => a.AddTime);
            roleIq = roleIq.Skip((pageIndex - 1) * pageSize).Take(pageSize);//分页前必须排序，不然EF报错

            return roleIq.ToList();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idArr"></param>
        public void ListDelete(string[] idArr)
        {
            CurrentDal.Delete(a => idArr.Contains(a.RoleId));
            CurrentDalSession.SaveChanges();
        }

        /// <summary>
        /// 获取角色权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Power> GetPower(string id)
        {
            List<Power> powerList=new List<Power>();
            Role role=CurrentDal.GetIQueryable(a=>a.RoleId.Equals(id)).Include("Powers").FirstOrDefault();

            if (role.Powers.Count > 0)
            {
                powerList = role.Powers.ToList();
            }

            return powerList;
        }

        /// <summary>
        /// 根据Id数组获取角色集合
        /// </summary>
        /// <param name="roleIdArr"></param>
        /// <returns></returns>
        public List<Role> GetRoleListByIdArr(string[] roleIdArr)
        {
            return CurrentDal.GetIQueryable().Where(a => roleIdArr.Contains(a.RoleId)).ToList();
        }

        /// <summary>
        ///  添加角色和Role_Power关系表
        /// </summary>
        /// <param name="pmRole"></param>
        /// <returns></returns>
        public bool AddRoleAndRole_Power(Role pmRole)
        {
            CurrentDal.Add(pmRole);
            CurrentDalSession.CurrentDbContext.ChangeTracker.DetectChanges();
            CurrentDalSession.SaveChanges();

            return true;
        }

        /// <summary>
        /// 更新角色和Role_Power关系表
        /// </summary>
        /// <param name="roleOld"></param>
        /// <returns></returns>
        public bool UpdateRoleAndRole_Power(Role roleOld)
        {
            CurrentDal.Update(roleOld);
            CurrentDalSession.CurrentDbContext.ChangeTracker.DetectChanges();
            CurrentDalSession.SaveChanges();

            return true;
        }

        /// <summary>
        /// 获取所有角色_异步
        /// </summary>
        /// <returns></returns>
        public List<Role> GetList_Async()
        {
            return CurrentDalSession.TimelyDbContext.Roles.ToList();
        }






        #region 私有方法
        private IQueryable<Role> BaseSearch(IQueryable<Role> roleIq, SMRole smRole)
        {
            if (!string.IsNullOrEmpty(smRole.RoleName))
            {
                roleIq = roleIq.Where(a => a.RoleName.Contains(smRole.RoleName));
            }
            if (smRole.AddTime != null)
            {
                DateTime dateTimeStart = DateTime.Parse(smRole.AddTime.Value.ToString("yyyy/MM/dd") + " 00:00:00");//Lambda不支持转换，要先转换好
                DateTime dateTimeEnd = DateTime.Parse(smRole.AddTime.Value.ToString("yyyy/MM/dd") + " 23:59:59");
                roleIq = roleIq.Where(a => a.AddTime.Value.CompareTo(dateTimeStart) >= 0 && a.AddTime.Value.CompareTo(dateTimeEnd) < 0);
            }
            return roleIq;
        }
        #endregion
        
    }
}
